package com.saturn.titan.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 过滤字符串中重复的词语
 * 
 * @author lwh
 * 
 */
public class WordsRepeatFilter {

	/**
	 * 实体抽取字符串中消除重复词语的过滤
	 * 
	 * @param sourceWords
	 * @return
	 */
	public static String entityWordsFilter(String sourceWords) {
		String[] source = sourceWords.replaceAll("<[\\S\\s]*?>", "").split(";");  //根据";"拆分原始实体字符串
		Set<String> set = null;
		List<String> list = null;
		String newString = "";
		for (int i = 0; i < source.length - 1; i++) {
			String entity = source[i].substring(source[i].indexOf(":") + 1,
					source[i].length() - 1);  //根据","拆分同一实体中的字符串，然后进行排重
			String entityName = source[i].substring(0, source[i].indexOf(":"));
			String[] entities = entity.split(",");
			list = new ArrayList<String>();
			for (int j = 0; j < entities.length - 1; j++) {
				list.add(entities[j]);
			}
			set = new HashSet<String>(list);  //通过set来对list进行排重
			Iterator<String> ite = set.iterator();
			newString = newString + entityName + ":";
	        for (int m = 0; ite.hasNext(); m++) {  //重新组装字符串
	        	if(m < set.size() - 1) {
	        		newString += ite.next() + ",";
	        	}else {
	        		newString += ite.next();
	        	}
	        }
	        if(i < source.length - 2) {
	        	newString = newString + ";";
	        }
		}
		return newString;
	}

}
